import doT from '../../../../vendor/template/doT.js';
import watermark from '../../../common/watermark.js';
import penentregsApi from '../../../common/api/food/penentregs.api.js'
import { requestFilePath } from "../../../common/api/file-request.js";

(function (window, document, $) {
    const lksalesmainTemplate = doT.template($('#looksalesplanmain-template').text());
    const looksalesTemplate = doT.template($('#looksalesplan-template').text());

    $(function () {
        $('body').attr('data-weui-theme', 'light'); //强制光明模式
        _watermarkBgImg();
        _initCommand()
    });

    //返回列表
    $(document).on('click', '#returnlist', function () {
        location.href = `./penentregslist.html?v=${Utils.GetTime()}`;
    });

    //命令初始化
    const _initCommand = async () => {
        let ids = getUrlKey("id")
        penentregsApi.getPenentregs(ids).then(async res => {
            if(Utils.IsNotNull(res?.pigEntAndCheckRegId)){
                await penentregsApi.getAnmlQtCertRegs(res?.pigEntAndCheckRegId).then(results=>{
                    res.pigEntAndCheckRegBillNo = results.bizFormObj.billNo;
                })
            }
            if (Utils.IsNotNull(res.attaches)) {
                _viewImages(res.attaches, `uploaderFiles`);
            }
            $('#looksalesMain').empty().append(lksalesmainTemplate(res));
            $('#looksalesEntry').empty().append(looksalesTemplate({ entreis: res.penEntEntry }));

        });
        // 查看猪只详情
        $(document).on('click', '#looksalesEntry .pigdetails', function () {
            let index = $(this).data('index')
            res.penEntEntry[index]

        })

    }
    // 附件回显
    const _viewImages = async (images, id) => {
        let _imag = images.split(',');
        if (_imag) {
            let _sun = 0;
            for (let i = 0; i < _imag.length; i++) {
                ++_sun;
                await requestFilePath(_imag[i]).then(_res => {
                    if (_res.isSucceed) {
                        let _li;
                        if (["jpg", "jpeg", "png", 'gif'].indexOf(_imag[i].suffix) < 0) {
                            _li = `<li class="weui-uploader__file"  data-id="${_sun}" data-url="${_res.data}" data-image-id="${_imag[i]}" style="background-image: url(${_res.data}) "></li>`;
                        } else {
                            _li = `<li class="weui-uploader__file" data-id="${_sun}" data-url="${_res.data}" data-image-id="${_imag[i]}" style="background-image: url(${_res.data}) "></li>`;

                        }
                        $(`#${id}`).append(_li);
                    }
                });
            }
            if (_sun == 3) {
                $(".weui-uploader__input-box").hide();
            }
        }
        $(document).on('click', `#${id}`, function (e) {
            let target = e.target;
            while (!target.classList.contains(`weui-uploader__file`) && target) {
                target = target.parentNode;
            }
            if (!target) return;
            //从图片对应的li标签中获得所需属性
            let gallery = weui.gallery($(target).attr('data-url'), {
                className: 'custom-name',
            });
            $('.weui-icon_gallery-delete').css('display', 'none')
        });
    }


    /** 获取地址栏的 参数  传入 字段返回值 */
    const getUrlKey = (name) => {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }

    // 添加水印信息
    const _watermarkBgImg = () => {
        let _userInfo = JSON.parse(Utils.GetSessionStorage("SNAccountEply")) || {};
        watermark.set(_userInfo.name + _userInfo.numCode);// 水印
    }

})(window, document, jQuery)